package com.example.mydata.service;


import com.example.mydata.Mapper.Usermapper;
import com.example.mydata.User;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class Reservice {

    @Resource
    private Usermapper usermapper;

    // 使用构造函数注入 Usermapper
    public Reservice(Usermapper usermapper) {
        this.usermapper = usermapper;
    }

    // 使用 @Cacheable 注解启用缓存
    @Cacheable(value = "userCache", key = "'allUsers'")
    public List<User> getAllUsers() {
        System.out.println("查询数据库：获取所有用户");
        return usermapper.findAll();
    }

    @Cacheable(value = "userCache", key = "#id")
    public User getUserName(Long id) {
        return usermapper.findUserNameById(id);
    }
}